package com.example.demo.service.impl;

import com.example.demo.dao.DesignerDao;
import com.example.demo.entity.Designer;
import com.example.demo.service.DesignerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class DesignerServiceImpl implements DesignerService {
    @Resource
    private DesignerDao designerDao;

    @Override
    public Map login(Designer designer) {
        Designer designer1 = designerDao.getDesignerById(designer.getDesId());
        Map map=new HashMap();
        if(designer1==null){
            map.put("msg",0);//该工号不存在
        }else {
            String password = designer.getPassword();
            String password1 = designer1.getPassword();
            if(password==password1 || password.equals(password1)){
                map.put("msg",2);
                map.put("designer",designer1);//登录成功
            }else {
                map.put("msg",1);//密码错误
            }
        }
        return map;
    }

    @Override
    public Map modifyPwd(String password, String newPwd, String desId) {
        Map map = new HashMap();
        Designer designer = designerDao.getDesignerById(desId);
        if(designer==null){
            map.put("msg",0);//用户不存在
        } else {
            String oldPwd = designer.getPassword();
            if(oldPwd==password || oldPwd.equals(password)){
                designerDao.modifyPwd(newPwd,desId);
                map.put("msg",1);//修改成功
            } else {
                map.put("msg",2);//旧密码错误
            }
        }
        return map;
    }

    @Override
    public Designer getDesignerByDesId(String desId) {
        return designerDao.getDesignerById(desId);
    }

}
